#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#第一章 python数据模型

import collections
from random import choice
from math import hypot

Card = collections.namedtuple("Card",['rank','suit'])

class FrenchDeck:
    ranks = [str(n) for n in range(2,11)] + list("JQKA")
    suits = "spades diamonds clubs hearts".split()
    def __init__(self):
        #得到一副卡牌
        self._cards = [Card(rank,suit) for rank in self.ranks for suit in self.suits] #相当于嵌套循环
    def __len__(self):
        return len(self._cards)
    def __getitem__(self,pos):
        return self._cards[pos]

deck = FrenchDeck()
print( choice(deck) ) #从序列中随机选取一个

class Vector:
    def __init__(self,x=0,y=0):
        self.x = x
        self.y = y
    def _makeStr(self):
        return "Vector("+str(self.x)+","+str(self.y)+")"

    def __repr__(self): # __str__是面向用户的，而__repr__面向程序员
        return "repr :" + self._makeStr()
    
    def __str__(self):#str() 函数被使用，或是在用print 函数打印一个对象的时候才被调用的
        return "str :"+ self._makeStr()

    def __mul__(self,scale): #实现乘法运算符
        return Vector(self.x*scale,self.y*scale)

    def __add__(self,other):#实现加法
        x = self.x + other.x
        y = self.y + other.y
        return Vector(x,y)
    def __abs__(self):
        return hypot(self.x,self.y) #求平方和的开平方

vec =Vector(3,4)
print(vec,abs(vec))

#字符串格式化的使用
print(str.format("我是谁{0},今天多大啦?{1[0]},来自哪里啊？{2[add]}",'kongfu',[26],dict(add='gd_zj')))







